﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GRV11;

namespace gpuimp.core
{
    /// <summary>
    /// CanvasManager keeps track of all open canvases.  This is where access and manipulation of open canvases can be achieved.
    /// </summary>
    public static class CanvasManager
    {
        private static int currentCanvas = 0;
        private static int nextID = 0;
        private static List<Canvas> openCanvases = new List<Canvas>();
        public static GR gr;

        public static List<Canvas> Canvases
        {
            get { return openCanvases; }
        }

        public static Canvas getCanvas(int canvasID)
        {
            return openCanvases.Find(delegate(Canvas c){
                return c.ID == canvasID;
            });      
        }

        /// <summary>
        /// Adds a canvas to the list
        /// </summary>
        /// <param name="imageData">Canvas's beginning image data</param>
        /// <param name="fileID">ImageFile ID</param>
        /// <param name="name">Name of Canvas</param>
        /// <returns>Canvas's ID</returns>
        public static int addCanvas(Bitmap imageData, int fileID, string name)
        {
            //check input parameters
            if (imageData == null || fileID < 0 || fileID > FileManager.getNumOpenFiles() || name == "")
            {
                return -1;
            }

            //create a new canvas, and add it to the list
            Canvas newCanvas = new Canvas(new Point(imageData.Width, imageData.Height),
                                          nextID,
                                          fileID,
                                          name);
            
            openCanvases.Add(newCanvas);
            currentCanvas = nextID;
            nextID++;

            Layer newLayer = new Layer(imageData);
            newCanvas.LayerManager.addLayer(newLayer);

            return currentCanvas;
        }

        public static Canvas CurrentCanvas
        {
            get { return getCanvas(getCurrentCanvas()); }
            
        }

        public static int CurrentCanvasNew
        {
            get { return CanvasManager.currentCanvas; }
            set { CanvasManager.currentCanvas = value; }
        }

        /// <summary>
        /// Adds the passed Canvas to the list
        /// </summary>
        /// <param name="canvas">Canvas to add</param>
        /// <returns>Canvas's ID</returns>
        public static int addCanvas(Canvas canvas)
        {
            if (canvas == null)
            {
                return -1;
            }

            canvas.ID = nextID;
            openCanvases.Add(canvas);
            currentCanvas = nextID;
            nextID++;

            return currentCanvas;
        }

        /// <summary>
        /// Removes the specified canvas from the list of current Canvases (Canvasi?)
        /// </summary>
        /// <param name="cID">the canvasID of the canvas to remove</param>
        public static void removeCanvas(int cID)
        {
            //invalid input
            if (cID < 0 || cID > nextID)
            {
                return;
            }

            //tell the FileManager this canvas is scheduled to be closed.
            if (openCanvases.Exists(delegate(Canvas c) { return c.ID == cID; }))
            {
                //if FileManager closed the file successfully, we can then remove it from the canvas list
                if (FileManager.closeFile(cID))
                {
                    openCanvases.Remove(openCanvases.Find(delegate(Canvas c) { return c.ID == cID; }));
                }
            }
        }

        /// <summary>
        /// Returns the current canvas
        /// </summary>
        /// <returns>Current Canvas's ID</returns>
        public static int getCurrentCanvas()
        {
            if (openCanvases.Count == 0)
                return -1;

            return currentCanvas;
        }
    }
}
